<template>
  <div class="app-container">
    <span> 装扮类型： </span>
    <template>
      <el-select v-model="status" placeholder="请选择状态"  style="margin: 10px">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </template>

    <el-button type="primary" plain @click="dialogCreateGiftVisible = true">添加装扮</el-button>
    <el-button type="primary" round @click="search">搜索</el-button>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="110">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="名称" width="150">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="图标" width="200" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.icon"
            :preview-src-list="[scope.row.icon]"
          />
        </template>
      </el-table-column>
      <el-table-column label="价格" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.price  }}
        </template>
      </el-table-column>
      <el-table-column label="类型" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.type === 1? '金币购买':'活动获取' }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"/>
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="110" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" round @click="dialogFormUpdateVisible = true; updateDecorationDTO = scope.row">
            修改装扮
          </el-button>
          <el-button size="mini" type="primary" round @click="dialogFormGiveVisible = true; giveShopDTO.shopId = scope.row.id">
            赠送装扮
          </el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            circle
            @click=" handelDelete(scope.row.id)"
          />
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加装扮" :visible.sync="dialogCreateGiftVisible" width="76%">
      <el-form ref="form" :model="decorationDTO" label-width="80px">
        <el-form-item label="装扮名称">
          <el-input v-model="decorationDTO.name" />
        </el-form-item>
        <el-form-item label="价格/天">
          <el-input-number v-model="decorationDTO.price" :min="1" :max="100000000" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="装扮类型">
          <el-select v-model="decorationDTO.category" placeholder="礼物类型" style="margin: 10px">
            <el-option
              v-for="item in options.filter(o=>o.value!=null)"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="装扮图标">
          <el-upload
            action="https://admin-api.pengxinmeta.com/admin-api/file/upload"
            list-type="picture-card"
            :limit="1"

            :on-success="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus" />
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="decorationDTO.icon" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="装扮特效">
          <el-upload
            class="upload-demo"
            action="https://admin-api.pengxinmeta.com/admin-api/file/upload"
            :on-remove="handleRemove2"
            :on-success="handlePictureCardPreview2"
            multiple
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="fileList">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传 svga 文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="decorationDTO.sort" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogCreateGiftVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAddGfit">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="赠送装扮" :visible.sync="dialogFormGiveVisible" width="76%">
      <el-form ref="form" :model="giveShopDTO" label-width="80px">
        <el-form-item label="用户账号">
          <el-input v-model="giveShopDTO.userAccount" />
        </el-form-item>
        <el-form-item label="天数">
          <el-input-number v-model="giveShopDTO.days" :min="1" :max="100000000" label="描述文字"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormGiveVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleGiveShop">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改装扮" :visible.sync="dialogFormUpdateVisible" width="76%">
      <el-form ref="form" :model="updateDecorationDTO" label-width="80px">
        <el-form-item label="装扮名称">
          <el-input v-model="updateDecorationDTO.name" />
        </el-form-item>
        <el-form-item label="价格/天">
          <el-input-number v-model="updateDecorationDTO.price" :min="1" :max="100000000" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="装扮类型">
          <el-select v-model="updateDecorationDTO.category" placeholder="礼物类型" style="margin: 10px">
            <el-option
              v-for="item in options.filter(o=>o.value!=null)"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="updateDecorationDTO.sort" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogCreateGiftVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleUpdateShop">确 定</el-button>
      </div>
    </el-dialog>

    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="size"
      :current-page="page"
      :total="total"
      @current-change="fetchData">
    </el-pagination>
  </div>
</template>

<script>
import { getList, deleteById, addDecoration ,updateDecoration, giveShop} from '@/api/shop'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      options: [{
        value: null,
        label: '全部'
      }, {
        value: '1',
        label: '头像框'
      }, {
        value: '2',
        label: '聊天气泡'
      }, {
        value: '3',
        label: '麦位音波'
      },{
        value: '4',
        label: '昵称挂件'
      },{
        value: '5',
        label: '入场特效'
      },{
        value: '6',
        label: '个人名牌'
      },{
        value: '7',
        label: '主页特效'
      },
      ],
      dialogFormUpdateVisible: false,
      fileList: [],
      list: null,
      listLoading: true,
      status: null,
      page: 1,
      size: 10,
      total: 0,
      giveShopDTO: {
        days: null,
        shopId: null,
        userAccount: null
      },
      updateDecorationDTO: {
      },
      decorationDTO: {
        name: '',
        sort: 0,
        status: false,
        icon: '',
        price: '',
        category: null,
        materialUrl: ''
      },
      type: 1,
      dialogCreateGiftVisible: false,
      dialogFormGiveVisible: false
    }
  },
  created() {
    this.fetchData(this.page)
  },
  methods: {
    handlePictureCardPreview2(response, file) {
      this.decorationDTO.materialUrl = response.data.url
      console.log(this.decorationDTO.materialUrl)
    },
    handlePictureCardPreview(response, file) {
      this.decorationDTO.icon = response.data.url
    },
    handleRemove2(file, fileList) {
      this.fileList = []
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    handleRemove(file) {
      this.decorationDTO.icon = ''
      console.log(file)
    },
    handleClick(tab, event) {
      this.fetchData(this.page)
    },
    handleGiveShop() {
      this.dialogFormGiveVisible = false
      giveShop({ account: this.giveShopDTO.userAccount,days: this.giveShopDTO.days, shopId: this.giveShopDTO.shopId }).then(res => {
        this.fetchData(this.page)
        this.$message({
          type: 'success',
          message: '赠送成功!'
        })
      })
    },
    handleAddGfit() {
      this.dialogCreateGiftVisible = false;
      this.fileList = [];
      addDecoration(this.decorationDTO).then(res => {
        this.fetchData(this.page)
        this.$message({
          type: 'success',
          message: '创建成功!'
        })
      })
    },
    handleUpdateShop() {
      this.dialogFormUpdateVisible = false
      updateDecoration(this.updateDecorationDTO).then(res => {
        this.fetchData(this.page)
        this.$message({
          type: 'success',
          message: '创建成功!'
        })
      })
    },
    search() {
      this.listLoading = true
      getList({ page: this.page, size: this.size, category: this.status }).then(response => {
        this.total = response.total
        console.log(this.page)
        this.list = response.data
        this.listLoading = false
      })
    },
    fetchData(val) {
      this.page = val
      this.listLoading = true
      getList({ page: this.page, size: this.size, category: this.status }).then(response => {
        this.total = response.total
        console.log(this.page)
        this.list = response.data
        this.listLoading = false
      })
    },
    handelDelete(id) {
      this.$confirm('此操作将删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById(id).then(() => {
          this.fetchData(this.page)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handelRealDelete(id) {
      this.$confirm('此操作将彻底删除该动态,请谨慎操作！', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        realDeleteById(id).then(() => {
          this.fetchData(this.page)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
